package com.hskn.hss.module.empPlan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.controller.BaseController;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.core.utils.poi.ExcelUtil;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.ExcelReaderDataException;
import com.hskn.hss.module.common.to.ExcelReaderTO;
import com.hskn.hss.module.empPlan.entity.EmpPlan;
import com.hskn.hss.module.empPlan.mapper.EmpPlanMapper;
import com.hskn.hss.module.empPlan.service.EmpPlanService;
import com.hskn.hss.module.empPlan.to.AttendanceCountTo;
import com.hskn.hss.module.empPlan.to.EmpPlanSummaryTableTO;
import com.hskn.hss.module.empPlan.to.EmpPlanTo;
import com.hskn.hss.module.empPlan.to.SelectEmpPlanTableTO;
import com.hskn.hss.module.empPlan.vo.ActualOperationListVo;
import com.hskn.hss.module.empPlan.vo.EmpPlanForExcelVO;
import com.hskn.hss.module.empPlan.vo.EmpPlanSummaryDetailTableVO;
import com.hskn.hss.module.empPlan.vo.EmpPlanSummaryTableVO;
import com.hskn.hss.module.empPlanEmployee.entity.EmpPlanEmployee;
import com.hskn.hss.module.empPlanEmployee.mapper.EmpPlanEmployeeMapper;
import com.hskn.hss.module.rptEmployeeWork.entity.RptEmployeeWork;
import com.hskn.hss.module.rptEmployeeWork.service.RptEmployeeWorkService;
import com.hskn.hss.module.workgroup.entity.WorkGroup;
import com.hskn.hss.module.workgroup.mapper.WorkGroupMapper;
import com.hskn.hss.module.workrule.entity.WorkRule;
import com.hskn.hss.module.workrule.mapper.WorkRuleMapper;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-11
 */
@Slf4j
@RestController
@RequestMapping("/hss/empPlan")
@Api(tags = {"-emp_plan"}, description = "人员排班计划")
public class EmpPlanController extends BaseController {

    @Autowired
    private EmpPlanService empPlanService;
    @Autowired
    private RptEmployeeWorkService rptEmployeeWorkService;


    @RequestMapping(value = "masterList", method = RequestMethod.POST)
    public AjaxResult<Object> masterList(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            BaseEntity entity = new BaseEntity();
            return AjaxResult.success(empPlanService.masterList(entity, empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "detailList", method = RequestMethod.POST)
    public AjaxResult<Object> detailList(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            BaseEntity entity = new BaseEntity();
            return AjaxResult.success(empPlanService.detailList(entity, empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "read", method = RequestMethod.POST)
    public AjaxResult<Object> read(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            return AjaxResult.success(empPlanService.read(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.INSERT, tableName = "emp_plan", bean = EmpPlan.class, beanMapper = EmpPlanMapper.class, names = "planName")
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public AjaxResult<Object> create(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            return AjaxResult.success(empPlanService.create(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "repeatPlanName", method = RequestMethod.POST)
    public AjaxResult<Object> repeatPlanName(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            return AjaxResult.success(empPlanService.repeatPlanName(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "emp_plan", idKey = "ids", bean = EmpPlan.class, beanMapper = EmpPlanMapper.class, names = "planName")
    @RequestMapping(value = "updateByDay", method = RequestMethod.POST)
    public AjaxResult<Object> updateByDay(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            return AjaxResult.success(empPlanService.updateByDay(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "emp_plan", idKey = "ids", bean = EmpPlan.class, beanMapper = EmpPlanMapper.class, names = "planName")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public AjaxResult<Object> update(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {

            return AjaxResult.success(empPlanService.update(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "emp_plan", idKey = "ids", bean = EmpPlan.class, beanMapper = EmpPlanMapper.class, names = "planName")
    @RequestMapping(value = "deleteMaster", method = RequestMethod.POST)
    public AjaxResult<Object> deleteMaster(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {

            return AjaxResult.success(empPlanService.deleteMaster(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "emp_plan_employee", idKey = "id", bean = EmpPlanEmployee.class, beanMapper = EmpPlanEmployeeMapper.class, names = "employeeId, planId")
    @RequestMapping(value = "deleteDetail", method = RequestMethod.POST)
    public AjaxResult<Object> deleteDetail(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            return AjaxResult.success(empPlanService.deleteDetail(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "work_rule", idKey = "workRuleIdList", bean = WorkRule.class, beanMapper = WorkRuleMapper.class, names = "ruleName")
    @RequestMapping(value = "deleteWorkRule", method = RequestMethod.POST)
    public AjaxResult<Object> deleteWorkRule(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            return AjaxResult.success(empPlanService.deleteWorkRule(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "work_group", idKey = "workGroupIdList", bean = WorkGroup.class, beanMapper = WorkGroupMapper.class, names = "name")
    @RequestMapping(value = "deleteWorkGroup", method = RequestMethod.POST)
    public AjaxResult<Object> deleteWorkGroup(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            return AjaxResult.success(empPlanService.deleteWorkGroup(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    @RequestMapping(value = "employeeConfig", method = RequestMethod.POST)
    public AjaxResult<Object> employeeConfig() {
        try {
            return AjaxResult.success(empPlanService.employeeConfig());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 网格作业概况汇总明细
     *
     * @param empPlanSummaryTableTO
     * @return
     */
    @ApiOperation(value = "网格作业概况汇总明细", notes = "网格作业概况汇总明细")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EmpPlanSummaryDetailTableVO.class)
    })
    @RequestMapping(value = "selectSummaryDetailTable", method = RequestMethod.POST)
    public AjaxResult selectSummaryDetailTable(@RequestBody @Validated EmpPlanSummaryTableTO empPlanSummaryTableTO) {
        try {
            return AjaxResult.success(Tools.buildTableDataInfo(empPlanSummaryTableTO.getPage(), empPlanSummaryTableTO.getSize(), empPlanService.selectSummaryDetailTable(empPlanSummaryTableTO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 网格作业概况汇总
     *
     * @param empPlanSummaryTableTO
     * @return
     */
    @ApiOperation(value = "网格作业概况汇总", notes = "网格作业概况汇总")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EmpPlanSummaryTableVO.class)
    })
    @RequestMapping(value = "selectSummaryTable", method = RequestMethod.POST)
    public AjaxResult selectSummaryTable(@RequestBody @Validated EmpPlanSummaryTableTO empPlanSummaryTableTO) {
        try {

            return AjaxResult.success(Tools.buildTableDataInfo(empPlanSummaryTableTO.getPage(), empPlanSummaryTableTO.getSize(), empPlanService.selectSummaryTable(empPlanSummaryTableTO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 排班计划导入
     *
     * @param excelReaderTO
     * @return
     */
    @SysLog(businessType = BusinessType.INSERT, tableName = "emp_plan_employee", bean = EmpPlanEmployee.class, beanMapper = EmpPlanEmployeeMapper.class, names = "employeeId, planId")
    @ApiOperation(value = "排班计划导入", notes = "排班计划导入")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EmpPlanForExcelVO.class)
    })
    @RequestMapping(value = "excelReader", method = RequestMethod.POST)
    public AjaxResult excelReader(@Validated ExcelReaderTO excelReaderTO) throws ExcelReaderDataException {
        return toAjax(empPlanService.excelReader(excelReaderTO));
    }

    /**
     * 道路保洁排班计划导出
     *
     * @param selectEmpPlanTableTO
     * @return
     */
    @ApiOperation(value = "道路保洁排班计划导出", notes = "道路保洁排班计划导出")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EmpPlanForExcelVO.class)
    })
    @RequestMapping(value = "selectEmpPlanForExcel", method = RequestMethod.POST)
    public com.hskn.hss.core.domain.AjaxResult selectEmpPlanForExcel(@RequestBody @Validated SelectEmpPlanTableTO selectEmpPlanTableTO) {
        try {
            List<EmpPlan> empPlanList = empPlanService.selectList(selectEmpPlanTableTO);
            List<EmpPlanForExcelVO> empPlanForExcelVOList = empPlanService.selectEmpPlanForExcel(empPlanList);
            ExcelUtil<EmpPlanForExcelVO> util = new ExcelUtil<>(EmpPlanForExcelVO.class);
            return util.exportExcel(empPlanForExcelVOList, "道路保洁排班计划");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return com.hskn.hss.core.domain.AjaxResult.error("操作失败");
        }
    }

    /**
     * 道路保洁排班计划导出模板
     *
     * @return
     */
    @ApiOperation(value = "道路保洁排班计划导出模板", notes = "道路保洁排班计划导出模板")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EmpPlanForExcelVO.class)
    })
    @RequestMapping(value = "selectEmpPlanForExcelTemplate", method = RequestMethod.POST)
    public com.hskn.hss.core.domain.AjaxResult selectEmpPlanForExcelTemplate() {
        try {
            ExcelUtil<EmpPlanForExcelVO> util = new ExcelUtil<>(EmpPlanForExcelVO.class);
            return util.exportExcel(new ArrayList<>(), "道路保洁排班计划模板");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return com.hskn.hss.core.domain.AjaxResult.error("操作失败");
        }
    }

    /**
     * 人员排班状态
     *
     * @param empPlanTo
     * @return
     */
    @ApiOperation(value = "人员排班状态", notes = "人员排班状态")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EmpPlanTo.class)
    })
    @RequestMapping(value = "getEmployeeShiftStatus", method = RequestMethod.POST)
    public AjaxResult getEmployeeShiftStatus(@RequestBody @Validated EmpPlanTo empPlanTo) {
        try {
            return AjaxResult.success(empPlanService.getEmployeeShiftStatus(empPlanTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 考勤汇总
     *
     * @param attendanceCountTo
     * @return
     */
    @ApiOperation(value = "考勤汇总", notes = "考勤汇总")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = AttendanceCountTo.class)
    })
    @RequestMapping(value = "attendanceCount", method = RequestMethod.POST)
    public AjaxResult attendanceCount(BaseEntity entity, @RequestBody @Validated AttendanceCountTo attendanceCountTo) {
        try {
            return AjaxResult.success(empPlanService.attendanceCount(entity, attendanceCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    /**
     * 考勤明细
     *
     * @param attendanceCountTo
     * @return
     */
    @ApiOperation(value = "考勤明细", notes = "考勤明细")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EmpPlanTo.class)
    })
    @RequestMapping(value = "attendanceDetail", method = RequestMethod.POST)
    public AjaxResult attendanceDetail(@RequestBody @Validated AttendanceCountTo attendanceCountTo) {
        try {
            return AjaxResult.success(empPlanService.attendanceDetail(attendanceCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 考勤图表
     *
     * @param attendanceCountTo
     * @return
     */
    @ApiOperation(value = "考勤图表", notes = "考勤图表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = EmpPlanTo.class)
    })
    @RequestMapping(value = "attendanceCountChars", method = RequestMethod.POST)
    public AjaxResult attendanceCountChars(@RequestBody @Validated AttendanceCountTo attendanceCountTo) {
        try {
            return AjaxResult.success(empPlanService.attendanceCountChars(attendanceCountTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 完成度列表
     *
     * @param selectEmpPlanTableTO
     * @return
     */
    @ApiOperation(value = "完成度列表", notes = "完成度列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ActualOperationListVo.class)
    })
    @RequestMapping(value = "getDegreeCompletionList", method = RequestMethod.POST)
    public AjaxResult getDegreeCompletionList(@RequestBody @Validated SelectEmpPlanTableTO selectEmpPlanTableTO) {
        try {
            QueryWrapper<RptEmployeeWork> rptEmployeeWorkQueryWrapper = new QueryWrapper<RptEmployeeWork>();
            rptEmployeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getWorkDate, Tools.getYearMonthDayString(new Date()));
            if (StringUtils.isNotEmpty(selectEmpPlanTableTO.getPlanName())) {
                rptEmployeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getPlanName, selectEmpPlanTableTO.getPlanName());
            }

            rptEmployeeWorkQueryWrapper.lambda().orderByDesc(RptEmployeeWork::getOverPointsNum);
//            rptEmployeeWorkQueryWrapper.lambda().ne(RptEmployeeWork::getOverPointsId, "").isNotNull(RptEmployeeWork::getOverPointsId);
            List<RptEmployeeWork> rptEmployeeWorkList = rptEmployeeWorkService.list(rptEmployeeWorkQueryWrapper);
            List<ActualOperationListVo> actualOperationListVoList = empPlanService.selectWorkKaoqinList(selectEmpPlanTableTO, rptEmployeeWorkList);
            actualOperationListVoList = actualOperationListVoList.stream()
                    .sorted(Comparator.comparing(ActualOperationListVo::getDegreeCompletionDecimal).reversed().
                            thenComparing(ActualOperationListVo::getPlanName).thenComparing(ActualOperationListVo::getStartTime).thenComparing(ActualOperationListVo::getEndTime)).collect(Collectors.toList());
            return AjaxResult.success(Tools.buildTableDataInfo(selectEmpPlanTableTO.getPage(), selectEmpPlanTableTO.getSize(), actualOperationListVoList));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


}

